/*
 * @(#)SubmissionSetTypeTransformer.java
 * Date 2013-09-25
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.registry.transformer.xds;

import java.util.List;

import javax.enterprise.context.ApplicationScoped;

import javax.inject.Inject;

import oasis.names.tc.ebxml_regrep.xsd.rim._3.ClassificationType;
import oasis.names.tc.ebxml_regrep.xsd.rim._3.ExternalIdentifierType;
import oasis.names.tc.ebxml_regrep.xsd.rim._3.RegistryPackageType;
import oasis.names.tc.ebxml_regrep.xsd.rim._3.SlotType1;

import org.joda.time.DateTime;

import us.hornerscorners.lamppost.exception.ServiceActionException;

import us.hornerscorners.lamppost.model.common.CodedElement;

import us.hornerscorners.lamppost.model.document.SubmissionSet;

import us.hornerscorners.lamppost.registry.constant.Metadata;
import us.hornerscorners.lamppost.registry.constant.SlotName;

import us.hornerscorners.lamppost.transformer.AbstractTransformer;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-09-25
 * @author         Jim Horner
 */
@ApplicationScoped
public class SubmissionSetTypeTransformer
        extends AbstractTransformer<SubmissionSet, RegistryPackageType> {

    /** Field description */
    @Inject
    private ClassificationTypeTransformer classificationTransformer;

    /** Field description */
    @Inject
    private ExternalIdentifierTypeTransformer externalIdentifierTransformer;

    /** Field description */
    @Inject
    private InternationalStringTypeTransformer internationalStringTransformer;

    /** Field description */
    @Inject
    private SlotTypeTransformer slotTransformer;

    /** Field description */
    @Inject
    private VersionInfoTypeTransformer versionInfoTransformer;

    /**
     * Constructs ...
     *
     */
    public SubmissionSetTypeTransformer() {

        super();
    }

    /**
     * Method description
     *
     *
     * @param subSet
     *
     * @return
     */
    @Override
    public RegistryPackageType transform(SubmissionSet subSet)
            throws ServiceActionException {

        RegistryPackageType result = new RegistryPackageType();

        // ***********
        // attributes

        result.setId(subSet.getEntryURN());
        result.setStatus(subSet.getAvailabilityStatus().getCode());
        result.setObjectType(Metadata.RegistryPackageObjectType.getURN());

        // versioning
        result.setLid(subSet.getEntryLogicalURN());
        result.setVersionInfo(
            this.versionInfoTransformer.transform(subSet.getEntryVersion()));

        // *******
        // slots
        List<SlotType1> slots = result.getSlot();

        DateTime submissionTime = subSet.getSubmissionTime();

        if (submissionTime != null) {

            slots.add(this.slotTransformer.transform(SlotName.SubmissionTime,
                    submissionTime));
        }

        List<String> intendedRecipients = subSet.getIntendedRecipients();

        if (intendedRecipients.isEmpty() == false) {

            slots.add(this.slotTransformer.transform(SlotName.IntendedRecipient,
                    intendedRecipients));
        }

        // ************
        // extra slots

        slots.addAll(this.slotTransformer.transform(subSet.getExtraAttributes()));

        // *********
        // elements

        result.setName(
            this.internationalStringTransformer.transform(subSet.getDisplayNames()));

        result.setDescription(
            this.internationalStringTransformer.transform(subSet.getDescriptions()));

        // **************
        // classification

        List<ClassificationType> classifications = result.getClassification();

        // submission set classification
        classifications.add(
            this.classificationTransformer.transformClassificationNode(
                subSet.getClassificationNode(), subSet));

        // author
        classifications.addAll(
            this.classificationTransformer.transformAuthors(
                Metadata.SubmissionSetAuthor, subSet, subSet.getAuthors()));

        // content type code

        CodedElement contentTypeCode = subSet.getContentTypeCode();

        if (contentTypeCode != null) {

            classifications.add(
                this.classificationTransformer.transformCodedElement(
                    Metadata.SubmissionSetContentTypeCode, subSet, contentTypeCode));
        }

        // *********************
        // external identifiers

        List<ExternalIdentifierType> externalIds = result.getExternalIdentifier();

        externalIds.add(
            this.externalIdentifierTransformer.transformPatientIdentifier(
                Metadata.SubmissionSetPatientId, subSet, subSet.getPatientId()));

        externalIds.add(
            this.externalIdentifierTransformer.transformPatientIdentifier(
                Metadata.SubmissionSetSourceId, subSet, subSet.getSourceId()));

        externalIds.add(
            this.externalIdentifierTransformer.transformUniqueIdentifier(
                Metadata.SubmissionSetUniqueId, subSet, subSet.getUniqueId()));

        return result;
    }
}
